package com.masterclient.app.businessmodule.minemodule.cash.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eoner.baselib.fragment.FCBaseFragment;
import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.eoner.common.config.BusinessConfig;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.minemodule.cash.detail.CashPayInfoActivity;
import com.masterclient.app.businessmodule.minemodule.cash.adapter.BalanceCashAdapter;
import com.masterclient.app.businessmodule.minemodule.cash.bean.BalanceCashBean;
import com.masterclient.app.businessmodule.minemodule.cash.bean.BalanceGainBean;
import com.masterclient.app.businessmodule.minemodule.cash.bean.BalanceOrderBean;
import com.masterclient.app.businessmodule.minemodule.cash.contract.CashContract;
import com.masterclient.app.businessmodule.minemodule.cash.contract.CashPresenter;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * 作者：sean
 * 创建时间：2017/10/12 15:16
 * 功能说明：
 */

public class BalanceCashFragment extends FCBaseFragment<CashPresenter> implements CashContract.View {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private BalanceCashAdapter balanceCashAdapter;
    private int total;

    @Override
    protected boolean useRefreshLayout() {
        return true;
    }

    @Override
    protected int requireGetLayoutId() {
        return R.layout.fragment_cash;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        navigationBar.setVisibility(View.GONE);
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        balanceCashAdapter = new BalanceCashAdapter();
        recyclerView.setAdapter(balanceCashAdapter);
        mPresenter.getBalanceCash("1", BusinessConfig.PAGE_SIZE,false);

        balanceCashAdapter.bindToRecyclerView(recyclerView);

        baseRefreshLayout().setOnRefreshListener(() -> {
            if (mPresenter.cashList != null) {
                mPresenter.cashList.clear();
                balanceCashAdapter.notifyDataSetChanged();
            }
            mPresenter.getBalanceCash("1", BusinessConfig.PAGE_SIZE,true);
        });
        balanceCashAdapter.setOnLoadMoreListener(() -> {
            if (mPresenter.cashList.size() < total) {
                mPresenter.getBalanceCash(String.valueOf(mPresenter.cashList.size() / Integer.valueOf(BusinessConfig.PAGE_SIZE) + 1), BusinessConfig.PAGE_SIZE,false);
            }
        }, recyclerView);
        balanceCashAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivitryWithBundle(CashPayInfoActivity.class, new String[][]{{"id", mPresenter.cashList.get(position).getSh_id() + ""}});
            }
        });
    }


    public void refreshList() {
        if (mPresenter.cashList != null) {
            mPresenter.cashList.clear();
            balanceCashAdapter.notifyDataSetChanged();
        }
        if (mPresenter != null)
            mPresenter.getBalanceCash("1", BusinessConfig.PAGE_SIZE,false);
    }

    @Override
    public void showBalanceGain(BalanceGainBean.DataBean bean) {

    }

    @Override
    public void showBalanceOrder(BalanceOrderBean.DataBean bean) {

    }

    @Override
    public void showBalanceCash(BalanceCashBean.DataBean bean) {
        if (baseRefreshLayout() != null && baseRefreshLayout().isRefreshing()) {
            baseRefreshLayout().setRefreshing(false);
        }

        total = Integer.valueOf(bean.getSh_total_rows());
        if (mPresenter.cashList == null) {
            mPresenter.cashList = new ArrayList<>();
        }
        mPresenter.cashList.addAll(bean.getSh_items());
        balanceCashAdapter.setNewData(mPresenter.cashList);
        if (mPresenter.cashList.size() >= total) {
            balanceCashAdapter.setEnableLoadMore(false);
        }

    }

    @Override
    public void loadFailed() {
        if (baseRefreshLayout() != null && baseRefreshLayout().isRefreshing()) {
            baseRefreshLayout().setRefreshing(false);
        }
        if (mPresenter.cashList!=null && (mPresenter.cashList.size()/Integer.valueOf(BusinessConfig.PAGE_SIZE)+1) > 1) {
            balanceCashAdapter.loadMoreFail();
        }
    }
}
